package com.example.madman.yuejian.Model;

import com.example.madman.yuejian.Bean.ClubBean;
import com.example.madman.yuejian.Listener.RetrofitListener;
import com.example.madman.yuejian.Listener.RetrofitService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;


/**
 * Created by Madman on 2018/10/8.
 */

public class ClubModel<T> {
    private Retrofit retrofit;
    private RetrofitService service;


    public ClubModel(String type) {
        if(type.equals("gson"))
        {
            retrofit = new Retrofit.Builder()
                    .baseUrl("http://192.168.155.1/yuejian/public/index.php/api/Club/clublist/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        else
        {
            retrofit = new Retrofit.Builder()
                    .baseUrl("http://192.168.155.1/yuejian/public/index.php/api/Club/clublist/")
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        }
        service = retrofit.create(RetrofitService.class);

    }
    public void getClubList(final RetrofitListener listener){
        Call<List<ClubBean>> call_a = service.getClubList();
        call_a.enqueue(new Callback<List<ClubBean>>() {
            @Override
            public void onResponse(Call<List<ClubBean>> call, Response<List<ClubBean>> response) {
                if (response.isSuccessful() && response.body() != null)
                    listener.onSuccess(response.body());
                else
                    listener.onFail("club_error");
            }
            @Override
            public void onFailure(Call<List<ClubBean>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });

    }
    public void getClubDetail(int clubid,final RetrofitListener listener2){
        Call<ClubBean> call_b = service.getClubDetail(clubid);
        call_b.enqueue(new Callback<ClubBean>() {
            @Override
            public void onResponse(Call<ClubBean> call, Response<ClubBean> response) {
                if (response.isSuccessful() && response.body() != null)
                    listener2.onSuccess(response.body());
                else
                    listener2.onFail("club_error");
            }
            @Override
            public void onFailure(Call<ClubBean> call, Throwable t) {
                listener2.onFail(t.toString());
            }
        });

    }
}
